package com.sskj.wallet.ui.activity;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.WalletCardBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.WalletManageActivityPresenter;
import com.sskj.wallet.uitl.TipWalletUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.WALLET_WALLET_MANAGE)//钱包管理
public class WalletManageActivity extends BaseActivity<WalletManageActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.btImport)
    Button btImport;
    @BindView(R2.id.btCreate)
    Button btCreate;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private SlimAdapter slimAdapter;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_wallet_manage;
    }

    @Override
    public WalletManageActivityPresenter getPresenter() {
        return new WalletManageActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.wallet_walletManageActivity1_j));
        DaggerUserDataComponent.create().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );

        slimAdapter = SlimAdapter.create().register(R.layout.wallet_recy_item_wallet_manage, new SlimInjector<WalletCardBean>() {
            @Override
            public void onInject(WalletCardBean data, IViewInjector injector, List payloads) {

                injector
                        .background(R.id.llWalletBg, ContextCompat.getDrawable(App.INSTANCE, data.getCoinEnum() == CoinEnum.BTC
                                ? R.drawable.wallet_shape_587_corner10 : R.drawable.wallet_shape_788_corner10))
                        .text(R.id.tvName, data.getTitle())
                        .text(R.id.tvAddress, data.getAddress())
                        .clicked(R.id.ivCopy, v -> {
                            CopyUtil.copy(data.getAddress());
                        })
                        .clicked(R.id.ivDDD, v -> {
                            ARouter.getInstance().build(RConfig.WALLET_WALLET_EDIT).withString(Constans.CODE, data.getCoinEnum().getCode()).navigation();
                        });

            }
        }).attachTo(recyclerView).updateData(new ArrayList());


    }

    @Override
    protected void initEvent() {
        ClickUtil.click(btImport, () -> {
            TipWalletUtil.showChoose(this, coinEnum -> {
                TipUtil.getSureCancelTip(this, App.INSTANCE.getString(R.string.wallet_walletManageActivity2_j), String.format(App.INSTANCE.getString(R.string.wallet_walletManageActivity3_j), coinEnum.getCode()), () -> {
                    ARouter.getInstance()
                            .build(RConfig.WALLET_IMPORT_WALLET)
                            .withSerializable(Constans.COIN, coinEnum)
                            .withBoolean(Constans.IS_FIRST, false)
                            .navigation();
                });
            });

        });
        ClickUtil.click(btCreate, () -> {
//            TipUtil.getSureCancelTip(this, App.INSTANCE.getString(R.string.wallet_walletManageActivity2_j), String.format(App.INSTANCE.getString(R.string.wallet_walletManageActivity3_j), CoinEnum.BTC.getCode() + App.INSTANCE.getString(R.string.wallet_walletManageActivity4_j) + CoinEnum.ETH.getCode()), () -> {
//                ARouter.getInstance().build(RConfig.WALLET_CREATE_WALLET).navigation();
//            });
            TipWalletUtil.showChoose(this, coinEnum -> {
                TipUtil.getSureCancelTip(this, App.INSTANCE.getString(R.string.wallet_walletManageActivity2_j), String.format(App.INSTANCE.getString(R.string.wallet_walletManageActivity3_j), coinEnum.getCode()), () -> {
                    ARouter.getInstance()
                            .build(RConfig.WALLET_CREATE_WALLET)
                            .withSerializable(Constans.COIN, coinEnum)
                            .withBoolean(Constans.IS_FIRST, false)
                            .navigation();

                });
            });
        });
    }

    @Override
    protected void initData() {
        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                }
                ArrayList<WalletCardBean> walletCardBeans = new ArrayList<>();
                walletCardBeans.add(new WalletCardBean(walletBeanBtc.getName(), walletBeanBtc.getAddr()).setCoinEnum(CoinEnum.BTC));
                walletCardBeans.add(new WalletCardBean(walletBeanEth.getName(), walletBeanEth.getAddr()).setCoinEnum(CoinEnum.ETH));
                slimAdapter.updateData(walletCardBeans);
            }
        });
    }
}
